#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpixmap.h"
#include <qstate.h>
#include "qstatemachine.h"
#include "qdesktopwidget.h"
#include "qprocess.h"
#include "qtextcodec.h"
#include "qchar.h"
#include <QTime>
#include "qlabel.h"




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    //process0->write()
    delete process0;
    delete ui;
}


//#define Mtr(s) (gbCode->toUnicode(s))

void MainWindow::start_default(MainWindow *wm)
{

    QDesktopWidget* desktopWidget = QApplication::desktop();
    screenrect = desktopWidget->screenGeometry();

//set the default arguments
    my_window = wm;
    current_dir = QDir::currentPath();
    qDebug("current path: %s", qPrintable(current_dir));

    //current_dir = "/home/linux001/qt_mplayer";

    audio_floor_flag ="1";
    display_pos ="1";
    parse_config();
    //elevator_num = "2";



    if(elevator_num == "2")
    {
    ratio_x = 7;
    ratio_y = 18;
    }
    else
    {
        ratio_x = 6;
        ratio_y = 18;
    }


    Q_INIT_RESOURCE(test1);
    gbCode = QTextCodec::codecForName("UTF-8");

   // label_time->palette();
    palette_white.setColor(QPalette::WindowText,QColor("white"));
    palette_red.setColor(QPalette::WindowText,QColor("red"));
    palette_yellow.setColor(QPalette::WindowText,QColor("yellow"));




    time_flag0 = 0;
    current_time = QTime::currentTime();

    //QPalette palette_time;
    //palette_time = label_time->palette();
    //palette_time.setColor(QPalette::Text,QColor(255,255,0));
    label_time->setPalette(palette_yellow);
    label_time->show();


    picture_load();
    set_widget_size();
    set_widget_bg();
    set_dis_config();

    disconnect(this,SIGNAL(emit_move()),this,SLOT(move_label()));
    disconnect(this,SIGNAL(emit_updatetime()),this,SLOT(update_time()));
    disconnect(this,SIGNAL(emit_read_fifo()),this,SLOT(read_fifo()));
    disconnect(this,SIGNAL(emit_roll_one_time()),this,SLOT(roll_disposal()));
    disconnect(this,SIGNAL(emit_roll_one_time_bottom()),this,SLOT(roll_disposal2()));
    //disconnect(this,SIGNAL(emit_read_uart_buf()),this,SLOT(read_uart_buf()));
    //disconnect(this,SIGNAL(emit_update_info()),this,SLOT(update_info()));
    disconnect(this,SIGNAL(emit_play_floor_info()),this,SLOT(play_floor_info()));

    disconnect(this,SIGNAL(emit_updatefloor()),this,SLOT(update_floor()));
    disconnect(this,SIGNAL(emit_change_direction()),this,SLOT(change_direction()));
    disconnect(this,SIGNAL(emit_update_spec_info()),this,SLOT(update_spec_info()));
    //disconnect(&elevator_uart1_thread,SIGNAL(emit_123()),this,SLOT(update_spec_info()));

    //disconnect(elevator_uart1_thread,SIGNAL(emit_update()),this,SLOT(update_info1()));
    disconnect(this,SIGNAL(emit_update_uart_1()),this,SLOT(update_info1()));
    disconnect(text_uart_thread,SIGNAL(emit_update()),this,SLOT(update_uart_context()));
    disconnect(process0,SIGNAL(finished(int)),this,SLOT(update_video_player()));
    disconnect(process1,SIGNAL(finished(int)),this,SLOT(video_adjust()));
    disconnect(process0,SIGNAL(readyReadStandardOutput()),this,SLOT(get_mplayer_pos()));



    connect(this,SIGNAL(emit_move()),this,SLOT(move_label()));
    connect(this,SIGNAL(emit_updatetime()),this,SLOT(update_time()));
    connect(this,SIGNAL(emit_read_fifo()),this,SLOT(read_fifo()));
    connect(this,SIGNAL(emit_roll_one_time()),this,SLOT(roll_disposal()));
    connect(this,SIGNAL(emit_roll_one_time_bottom()),this,SLOT(roll_disposal2()));
    //connect(this,SIGNAL(emit_read_uart_buf()),this,SLOT(read_uart_buf()));
    //connect(this,SIGNAL(emit_update_info()),this,SLOT(update_info()));
    connect(this,SIGNAL(emit_play_floor_info()),this,SLOT(play_floor_info()));

    connect(this,SIGNAL(emit_updatefloor()),this,SLOT(update_floor()));
    connect(this,SIGNAL(emit_change_direction()),this,SLOT(change_direction()));
    connect(this,SIGNAL(emit_update_spec_info()),this,SLOT(update_spec_info()));
    //connect(&elevator_uart1_thread,SIGNAL(emit_123()),this,SLOT(update_spec_info()));

    //connect(elevator_uart1_thread,SIGNAL(emit_update()),this,SLOT(update_info1()));
    //connect(this,SIGNAL(emit_update_uart_1()),this,SLOT(update_info1()));
    connect(text_uart_thread,SIGNAL(emit_update()),this,SLOT(update_uart_context()));
    connect(process0,SIGNAL(finished(int)),this,SLOT(update_video_player()));
    connect(process1,SIGNAL(finished(int)),this,SLOT(video_adjust()));
    connect(process0,SIGNAL(readyReadStandardOutput()),this,SLOT(get_mplayer_pos()));



    if(elevator_num == "2")
    {
        disconnect(this,SIGNAL(emit_updatefloor2()),this,SLOT(update_floor2()));
        disconnect(this,SIGNAL(emit_change_direction2()),this,SLOT(change_direction2()));
        disconnect(this,SIGNAL(emit_update_spec_info2()),this,SLOT(update_spec_info2()));
        //disconnect(elevator_uart2_thread,SIGNAL(emit_update()),this,SLOT(update_info2()));
        disconnect(this,SIGNAL(emit_update_uart_2()),this,SLOT(update_info2()));
        disconnect(process1,SIGNAL(finished(int)),this,SLOT(video_adjust()));



        connect(this,SIGNAL(emit_updatefloor2()),this,SLOT(update_floor2()));
        connect(this,SIGNAL(emit_change_direction2()),this,SLOT(change_direction2()));
        connect(this,SIGNAL(emit_update_spec_info2()),this,SLOT(update_spec_info2()));
        //connect(elevator_uart2_thread,SIGNAL(emit_update()),this,SLOT(update_info2()));
        //connect(this,SIGNAL(emit_update_uart_2()),this,SLOT(update_info2()));
        disconnect(process1,SIGNAL(finished(int)),this,SLOT(video_adjust()));
        //disconnect(elevator_uart2_thread,SIGNAL(emit_update()),this,SLOT(update_info()));
    }
    //connect(&timer0,SIGNAL(time_out()),this,SLOT(video_adjust()));

        this->setCursor(Qt::BlankCursor);   //隐藏鼠标
        //this->setCursor(Qt::ArrowCursor);  //显示正常鼠标

        set_fonts();

        read_caption_file();
        get_floor_real_name();



#if 0
       elevator_uart1_thread->timer_0 = uart0_timer;
       elevator_uart1_thread->time_out_flag=&time_out_flag1;

       connect(uart0_timer, SIGNAL(timeout()), elevator_uart1_thread, SLOT(update_timer()));

        process1->setProcessChannelMode(QProcess::MergedChannels);

        elevator_uart1_thread->elevator_info_uart_1 = elevator_info_uart_1;
        elevator_uart1_thread->uart_fisrt_receive = 1;
        elevator_uart1_thread->uart_fd=Init_serial(elevator_info_uart_1);
       // if(elevator_uart1_thread->uart_fd<0)
        //    exit(1);
        //close(fd_uart);
        if(system_verify_flag == "verify passed")
            elevator_uart1_thread->start();

        if(elevator_num =="2")
        {
            uart1_timer = new QTimer;
            elevator_uart2_thread->time_out_flag=&time_out_flag2;

            elevator_uart2_thread->timer_0=uart1_timer;
            //connect(uart1_timer, SIGNAL(timeout()), elevator_uart2_thread, SLOT(update_timer()));
            elevator_uart2_thread->uart_fisrt_receive = 1;

            elevator_uart2_thread->elevator_info_uart_1 = elevator_info_uart_2;
            elevator_uart2_thread->uart_fd=Init_serial(elevator_info_uart_2);

           // if(elevator_uart2_thread->uart_fd<0)
            //    exit(1);
            if(system_verify_flag == "verify passed")
                elevator_uart2_thread->start();
        }


        if(system_verify_flag != "verify passed")
        {
            //disconnect(this,SIGNAL(emit_system_verify()),this,SLOT(do_system_verify()));

            //connect(this,SIGNAL(emit_system_verify()),this,SLOT(do_system_verify()));
            //QTimer::singleShot(1000,this,SLOT(do_system_verify()));

        }
#endif
        video_codec_check();

        ui->frame->show();
        ui->frame_2->show();
        label_week->show();
        label_date->show();
        label_time->show();
        ui->label_4->show();
        ui->label_1->show();
        label_spec_info->show();
        label_spec_info1->show();

        if(elevator_num != "2")
        {
            //ui->label->hide();
            //ui->label_1->hide();
            ui->label_2->hide();
            ui->label_3->hide();
            ui->label_5->hide();
            label_spec_info1->hide();
            label_floor1->hide();
            up_frame2->hide();
            down_frame2->hide();
            //down_up_frame1->hide();
        }

        source_dir = "/media/SUNUP/sunup";

        QTimer::singleShot(1000,this, SLOT(system_timer_0()));
        QTimer::singleShot(1000,this,SLOT(check_mplayer_stat()));


}

void MainWindow::new_start()
{


}

void MainWindow::ui_on_top()
{
    //ui->centralWidget->showFullScreen();
    QTimer::singleShot(100,this,SLOT(ui_on_top()));

}

void MainWindow::copy_label_flush()
{

#if 0
    flush_flag ++;
    if(flush_flag >2)
    {
        ui->label_1->hide();
    }
    else
    {
        ui->label_1->show();
    }

    if(flush_flag > 10)
        flush_flag = 0;
#endif
    //screenrect
    //label_time->topLevelWidget();
    //ui->label_3->topLevelWidget();
    ui->label_3->setFixedWidth((current_read_bytes*screenrect.width())/all_bytes);
    ui->label_3->show();
    my_window->showFullScreen();

}

void MainWindow::move_label()
{
    //my_window->showFullScreen();

    //QPalette palette_roll;

    if(elevator_num == "2")
    {

    }

    //bottom_move_pos = bottom_move_pos-3;

    //qDebug("lable bottomleft:%d ",ui->label->geometry().bottomRight().x());
    //if(bottom_move_pos>-(ui->label_1->geometry().width()))
        //ui->label_1->move(bottom_move_pos,screenrect.height()*17/18);
    if(ui->label->geometry().bottomRight().x() > 0)
        ui->label->move(ui->label->geometry().topLeft()-QPoint(3,0));
    else
    {
        //bottom_move_pos = screenrect.width();
        ui->label->hide();
        ui->label->move(ui->label_1->geometry().topRight().x(),ui->label->geometry().topLeft().y());
        ui->label->show();


        //if(uart_context_flag == 1)
        if (0)
        {

        }
        else
        {
            emit(emit_roll_one_time_bottom());
        }
    }


    if(ui->label_1->geometry().bottomRight().x() > 0)
        ui->label_1->move(ui->label_1->geometry().topLeft()-QPoint(3,0));
    else
    {
        //bottom_move_pos = screenrect.width();
        ui->label_1->hide();
        ui->label_1->move(ui->label->geometry().topRight().x(),ui->label_1->geometry().topLeft().y());
        ui->label_1->show();


        //if(uart_context_flag == 1)
        if (0)
        {

        }
        else
        {
            emit(emit_roll_one_time_bottom());
        }
    }
    //QPalette palette_roll;

    //palette_roll = ui->label_1->palette();
    //palette_roll.setColor(QPalette::WindowText,QColor("white"));
    ui->label_1->setPalette(palette_white);
    ui->label->setPalette(palette_white);


    //ui->label_1->setStyleSheet("background-color: red");

}

void MainWindow::update_time()
{
    current_time = QTime::currentTime();
    current_date = QDate::currentDate();

    label_time->setText(current_time.toString());
    //QPalette palette_time;
    //palette_time = label_time->palette();
    //palette_time.setColor(QPalette::WindowText,QColor(255,255,0));
    label_time->setPalette(palette_yellow);
    label_week->setPalette(palette_yellow);
    //if(date0 != current_date)
    if(1)
    {
    QString tmp;
    tmp = QString::number(current_date.year()) + "-" + QString::number(current_date.month()) + "-" + QString::number(current_date.day()) ;
    label_date->setText(tmp);
    //QPalette palette_date;
    //palette_date = label_date->palette();
    //palette_date.setColor(QPalette::WindowText,QColor(255,255,0));
    label_date->setPalette(palette_yellow);

    if(week_dis_flag == 1)
    {
    if(current_date.dayOfWeek()==1)
        label_week->setText(tr("星期一"));
    else if(current_date.dayOfWeek()==2)
        label_week->setText(tr("星期二"));
    else if(current_date.dayOfWeek()==3)
        label_week->setText(tr("星期三"));
    else if(current_date.dayOfWeek()==4)
        label_week->setText(tr("星期四"));
    else if(current_date.dayOfWeek()==5)
        label_week->setText(tr("星期五"));
    else if(current_date.dayOfWeek()==6)
        label_week->setText(tr("星期六"));
    else
        label_week->setText(tr("星期日"));
    }
    else
    {
        if(current_date.dayOfWeek()==1)
            label_week->setText(tr("Monday"));
        else if(current_date.dayOfWeek()==2)
            label_week->setText(tr("Tuesday"));
        else if(current_date.dayOfWeek()==3)
            label_week->setText(tr("Wednesday"));
        else if(current_date.dayOfWeek()==4)
            label_week->setText(tr("Thursday"));
        else if(current_date.dayOfWeek()==5)
            label_week->setText(tr("Friday"));
        else if(current_date.dayOfWeek()==6)
            label_week->setText(tr("Saturday"));
        else
            label_week->setText(tr("Sunday"));
    }


    label_week->setPalette(palette_yellow);
    date0 = QDate::currentDate();
    }


    label_week->setPalette(palette_yellow);
    label_date->setPalette(palette_yellow);


}

void MainWindow::change_direction2()
{

    if(elevator_info_state2.direction == 0)
    {
        down_frame2->hide();
        up_frame2->show();
    }
    else if(elevator_info_state2.direction == 1)
    {
        up_frame2->hide();
        down_frame2->show();
    }
    else
    {
        up_frame2->hide();
        down_frame2->hide();
    }



}

void MainWindow::change_direction()
{

    if(elevator_info_state1.direction == 0)
    {
        down_frame1->hide();
        up_frame1->show();
    }
    else if(elevator_info_state1.direction == 1)
    {
        up_frame1->hide();
        down_frame1->show();
    }
    else
    {
        up_frame1->hide();
        down_frame1->hide();
    }


}

char MainWindow::system_timer_0()
{
    if(QFile::exists(source_dir))
    {
        qDebug("check u disk");
        check_u_disk(0);
    }
    else
    {
        QTimer::singleShot(100,this, SLOT(system_timer_0()));
    }

    //emit(emit_update_info());
    time_flag0 ++;
    if(    time_flag0%1==0)
        emit(emit_move());

    if(time_flag0%10 ==0)
    {
        emit(emit_updatetime());
    }

#if 0
    if(time_flag0 > 1000)
    {
        time_flag0 = 1;
        if(system_verify_flag != "verify passed")
        {
            emit(emit_system_verify());
        }
    }

    if(elevator_uart1_thread->received_flag_uart1)
    {
        if(++time_out_flag1>=6)
        {
            time_out_flag1 = 0;
            elevator_uart1_thread->received_flag_uart1 = 0;
            elevator_uart1_thread->receive_uart_data();
            emit(emit_update_uart_1());
        }

    }


    if((elevator_uart2_thread->received_flag_uart2)&&(elevator_num == "2"))
    {
        if(++time_out_flag2>=6)
        {
            time_out_flag2 = 0;
            elevator_uart2_thread->received_flag_uart2 = 0;
            elevator_uart2_thread->receive_uart_data();
            emit(emit_update_uart_2());
        }

    }
#endif


    //debug_emit();
    return 0;
}

void MainWindow::timerEvent(QTimerEvent *event0)
{
    //emit(emit_update_info());
    qDebug("timeout");
    time_flag0 ++;
    if(    time_flag0%20==0)
        emit(emit_move());

    if(time_flag0%200 ==0)
    {
        check_u_disk(event0->timerId());
        emit(emit_updatetime());
    }


    if(time_flag0 > 1000)
    {
        time_flag0 = 1;
        if(system_verify_flag_1 != "verify passed")
        {
            emit(emit_system_verify());
        }


    }
#if 0
    if(elevator_uart1_thread->received_flag_uart1)
    {
        if(++time_out_flag1>=6)
        {
            time_out_flag1 = 0;
            elevator_uart1_thread->received_flag_uart1 = 0;
            elevator_uart1_thread->receive_uart_data();
            emit(emit_update_uart_1());
        }

    }


    if((elevator_uart2_thread->received_flag_uart2)&&(elevator_num == "2"))
    {
        if(++time_out_flag2>=6)
        {
            time_out_flag2 = 0;
            elevator_uart2_thread->received_flag_uart2 = 0;
            elevator_uart2_thread->receive_uart_data();
            emit(emit_update_uart_2());
        }

    }
#endif

    debug_emit();
}


char do_copy_file(char *p_source, char *p_des, unsigned int *current_read_bytes, unsigned int all_bytes)
{

#if 0
    FILE *fd_source;
    FILE *fd_des;
    char buf_tmp[40960];
    //qint64 byte_counter;
    //byte_counter = 0;
    qint64 read_bytes;
    //double copy_finished;

    *current_read_bytes = 0;
    //fd_source = open(p_source,O_RDONLY);
    fd_source = fopen(p_source,"r");

    if(fd_source < 0)
        qDebug("open 123.dat fail");

    //fd_des = open(p_des,O_WRONLY|O_CREAT,"0777");
    fd_des = fopen(p_des,"w+");
    if(fd_des < 0)
        qDebug("open 222.txt fail");
    do
    {
        //qDebug("1111111111111");

        //read_bytes = fread(fd_source,buf_tmp,40960);
        read_bytes = fread(buf_tmp,1,1024,fd_source);
        //sleep(1);
        //qDebug("bytes:%f",*current_read_bytes/(all_bytes*1.0));
        *current_read_bytes += read_bytes;

        if(read_bytes > 0)
        {
            //write(fd_des,buf_tmp,read_bytes);
            fwrite(buf_tmp,1,read_bytes,fd_des);
        }

    }while(read_bytes);
    fclose(fd_source);
    fclose(fd_des);

#endif

}


void MainWindow::restart_video_player()
{
    qDebug("into restart system");
    up_frame1->hide();
    up_frame2->hide();
    down_frame1->hide();
    down_frame2->hide();
    label_copy_schedule->hide();
    if(QFile::exists(source_dir))
    {
        QTimer::singleShot(200, this, SLOT(restart_video_player()));
    }
    else
    {
        qDebug("into restart system");
                //new_start();
        disconnect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files(int)));
        disconnect(cp_process,SIGNAL(readyRead()),this,SLOT(update_copy_info));

        start_default(my_window);
        //exit(0);
    }
}

void MainWindow::copy_files(void)
{


    qDebug("files_counter :%d", files_counter);
    if(files_counter < update_files.count())
    {
        int ii = 2;
        QString tmp_line;
        QString exe_cp_file;

        exe_cp_file= update_files.at(files_counter);
        //QPalette palette_floor;
        //palette_floor = ui->label_1->palette();
        //palette_floor.setColor(QPalette::WindowText,QColor("red"));
        ui->label_1->setPalette(palette_red);

        QString do_one_time_copy;
        tmp_line = exe_cp_file.split(" ").at(ii);
        do_one_time_copy = "./copy " + exe_cp_file.split(" ").at(ii) + " ";
        ii = 3;
        do_one_time_copy = do_one_time_copy + exe_cp_file.split(" ").at(ii);
        qDebug("%s", qPrintable(do_one_time_copy));

        cp_process->start(do_one_time_copy);
        //copy_file_timer->start(100);

        ui->label_3->setText(tmp_line);

        //cp_process->start(exe_cp_file);
        ui->label_1->setText("");
        //fd_pipe = open("/tmp/com_fifo",O_RDONLY);

        ui->label_1->show();
        ui->label_3->show();

        //QTimer::singleShot(5,this,SLOT(update_copy_info()));

    }
    else
    {

        ui->label_1->setText(tr(""));
        ui->label_5->setText(tr("拷贝结束，请拔出U盘"));
        disconnect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files(int)));

        //cp_process->start("echo 55 > /tmp/update.txt");
#if 0
        QFile file("/tmp/update.txt");
             if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                 return;

             QTextStream out(&file);
             out << "5";
#endif
        QTimer::singleShot(3000, this, SLOT(restart_video_player()));
    }

    files_counter ++;


}

bool removeDirectory(QString dirName)
{
 QDir dir(dirName);
 QString tmpdir = "";

 if(!dir.exists()){
   return false;
 }

 QFileInfoList fileInfoList = dir.entryInfoList();
 foreach(QFileInfo fileInfo, fileInfoList){
   if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
     continue;

   if(fileInfo.isDir()){
     tmpdir = dirName + ("/") + fileInfo.fileName();
     removeDirectory(tmpdir);
     dir.rmdir(fileInfo.fileName()); /**< 移除子目录 */
   }
   else if(fileInfo.isFile()){
     QFile tmpFile(fileInfo.fileName());
     dir.remove(tmpFile.fileName()); /**< 删除临时文件 */
   }
   else{
     ;
   }
 }

 dir.cdUp();            /**< 返回上级目录，因为只有返回上级目录，才可以删除这个目录 */
 if(dir.exists(dirName)){
   if(!dir.rmdir(dirName))
     return false;
 }
 return true;
}

void MainWindow::screen_change_to_update_state()
{
    qDebug("files dir exist!!!!!!");
    //killTimer(tt);
    ui->frame->hide();
    ui->label_1->hide();
    ui->label->hide();
    ui->frame_2->hide();
    ui->label_4->hide();
    ui->label_3->hide();

    label_time->hide();
    label_spec_info->hide();
    label_floor->hide();
    label_week->hide();
    label_date->hide();
    logo_frame->hide();
    up_frame1->hide();
    down_frame1->hide();
    //elevator_uart1_thread->terminate();


    if(elevator_num == "2")
    {
    label_spec_info1->hide();
    label_floor1->hide();
    up_frame2->hide();
    down_frame2->hide();
    }
    files_counter = 0;
    //ui->label_1->setStyleSheet("background-color: black");

    ui->label_2->setStyleSheet("background-color: black");
    ui->label_3->setStyleSheet("background-color: black");
    ui->label_5->move(0,0);//提示信息
    ui->label_2->move(0,0);//背景
    label_copy_schedule->move(0,screenrect.height()*8/10);//正在拷贝进度
    ui->label_3->move(0,screenrect.height()*9/10);//正在拷贝提示
    ui->label_2->setFixedSize(screenrect.width(), screenrect.height());
    label_copy_schedule->setFixedSize(screenrect.width(), screenrect.height()/10);
    ui->label_3->setFixedSize(screenrect.width(), screenrect.height()/10);
    ui->label_2->show();
    //ui->label_1->show();
    ui->label_2->setText(" ");
    disconnect(process0,SIGNAL(finished(int)),this,SLOT(update_video_player()));
    disconnect(process1,SIGNAL(finished(int)),this,SLOT(video_adjust()));
    process0->terminate();
    disconnect(copy_file_timer, SIGNAL(timeout()), this, SLOT(copy_label_flush()));
    //connect(copy_file_timer, SIGNAL(timeout()), this, SLOT(copy_label_flush()));
    disconnect(cp_process,SIGNAL(readyRead()),this,SLOT(update_copy_info));
    disconnect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files()));

    connect(cp_process,SIGNAL(readyRead()),this,SLOT(update_copy_info()));
    connect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files()));


    ui->label_5->setAlignment(Qt::AlignCenter);
    label_copy_schedule->setAlignment(Qt::AlignCenter);
    ui->label_3->setAlignment(Qt::AlignCenter);
    //ui->centralWidget->setStyleSheet("background-color: black");
    //ui->label_2->hide();

    QString fonts_size;
    ui->label_5->setFixedSize(screenrect.width(), screenrect.height());

    fonts_size.sprintf("font-size : %dpx", ui->label_5->geometry().height()/10);
    ui->label_5->setStyleSheet(fonts_size);

    fonts_size.sprintf("font-size : %dpx", ui->label_3->geometry().height()/2);
    ui->label_3->setStyleSheet(fonts_size);

    fonts_size.sprintf("font-size : %dpx", label_copy_schedule->geometry().height()/2);
    label_copy_schedule->setStyleSheet(fonts_size);

    ui->label_5->setText(tr("文件更新中，请稍候。。。。。"));

    //QPalette palette_floor;
    //palette_floor = ui->label_5->palette();
    //palette_floor.setColor(QPalette::WindowText,QColor("yellow"));
    ui->label_5->setPalette(palette_yellow);
    ui->label_5->show();

}

void MainWindow::update_copy_info(void)
{
    //QByteArray ba0;
    //ba0 = cp_process->readLine(42);
    QString str00;
    label_copy_schedule->show();
    str00 = cp_process->readLine(42);
    if(str00.at(0) == 'r')
    label_copy_schedule->setText(str00);
    qDebug("%s",qPrintable(str00));
    //ui->label_1->hide();
    label_copy_schedule->setPalette(palette_red);

    qDebug("copy file .....");

#if 0
    QString tem_str;
    char tmp[64];
    int read_byte;
    memset(tmp,0,64);
    //tem_str = my_stream->readLine(64);
    read_byte = read(fd_pipe,tmp,40);

    if(read_byte)
    {
        tem_str.sprintf("%s",tmp);
        ui->label_1->setText(tem_str);
        QTimer::singleShot(5,this,SLOT(update_copy_info()));
    }
    else
    {
        qDebug("finish copy");
        my_close(fd_pipe);
        copy_files(0);
    }
#endif
}



void MainWindow::check_u_disk(int tt)
{
    QString des_dir = current_dir;
    QString sourceDir = source_dir;

    //sourceDir = ;
    //source_dir = sourceDir;
    update_files.clear();
    QDesktopWidget* desktopWidget = QApplication::desktop();        
    screenrect = desktopWidget->screenGeometry();
    if(QFile::exists(source_dir))
    {

        screen_change_to_update_state();
        QString file_name;
        file_name = sourceDir + "/video_list.txt";
        QFile file01(file_name);
         if ( file01.open(QIODevice::ReadOnly) )
         {
                QTextStream stream( &file01);
                QString line;
                QString f_name;
                QChar *char0;
                stream.setCodec("UTF-8");
    #if 1
                while ( !stream.atEnd())
                {
                    line = stream.readLine(); // 不包括“\n”的一行文
                    line = line.remove(" ");
                    //qDebug("line %s",line.at(0).toAscii());
                    if(line == "")
                    continue;
                    char0 = line.data();
                    if((*char0 == '#') && (*(char0+1) == '#'))
                        continue;
                    else
                    {
                        f_name = line;
                        line = sourceDir + "/" + line;
                        if(QFile::exists(line))
                        {
                            line = "cp -r " + line + " ";
                            line = line + des_dir + "/video/" + f_name;
                            qDebug("%s", qPrintable(line));
                            update_files += line;
                        }
                    }
                }
    #endif
             file01.close();
             line = sourceDir + "/video_list.txt";
             line = "cp -r " + line + " ";
             line = line + des_dir + "/file/video_list.txt";
             qDebug("%s", qPrintable(line));
             update_files += line;
             if(update_files.count()>1)
                 {
                    QString rmdir_line;
                    QString mkdir_line;
                    rmdir_line = des_dir + "/video";
                    removeDirectory(rmdir_line);
                    mkdir_line = des_dir + "/video";
                    QDir my_mkdir(mkdir_line);
                    my_mkdir.mkdir(mkdir_line);
                 }
             }
         else
           qDebug("open video_list fail!\n");


/*cp the file caption_bottom*/

         QString line01;
         QString line_tmp;

         line01 = sourceDir + "/caption_bottom.txt";
         if(QFile::exists(line01))
         {
             line01 = "cp -r " + line01 + " " + des_dir + "/file/caption_bottom.txt";
             qDebug("%s", qPrintable(line01));
             line_tmp = des_dir + "/file/caption_bottom.txt";
             QFile::remove(line_tmp);
             update_files += line01;

         }
         else
         {
             qDebug("the caption_bottom.txt is not exist");

         }

         QString line02;

         line02 = sourceDir + "/logo.jpg";

         if(QFile::exists(line02))
         {
             line02 = "cp -r " + line02 + " " + des_dir + "/images/logo.jpg";
             //qDebug("%s", qPrintable(line02));
             update_files += line02;
         }
         else
         {
             qDebug("logo picture is not exist");
         }


         QString line03;
         line03 = sourceDir + "/config.conf";
         if(QFile::exists(line03))
         {
             line03 = "cp -r " + line03 + " " + des_dir + "/file/config.conf";
             qDebug("%s", qPrintable(line03));
             update_files += line03;

         }
         else
         {
             qDebug("logo picture is not exist");
         }

         QString line04;
         line04 = sourceDir + "/floor_name.txt";
         if(QFile::exists(line04))
         {
             line04 = "cp -r " + line04 + " " + des_dir + "/file/floor_name.txt";
             qDebug("%s", qPrintable(line04));
             update_files += line04;

         }
         else
         {
             qDebug("floor_name.txt is not exist");
         }


         qDebug("update_files number:%d ",update_files.count());

         /*begin to copy files*/
         cp_process->setProcessChannelMode(QProcess::MergedChannels);
         //cp_process->setReadChannel(QProcess::StandardOutput);

         //disconnect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files(int)));
         //connect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files(int)));

         if(update_files.count() > 0)
         {
                files_counter = 0;
                copy_files();
         }
         else
         {
             ui->label_5->setText(tr("没有更新文件，请拔出U盘!"));
             //cp_process->start("echo 5 > /tmp/update.txt");
             disconnect(cp_process,SIGNAL(finished(int)),this,SLOT(copy_files(int)));
                QTimer::singleShot(3000, this, SLOT(restart_video_player()));
         }
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{



}



void MainWindow::debug_emit()
{

    debug_counter++;
#if 0
    if(debug_counter<1600)
    {
        elevator_uart1_thread->elevator_info_state.spec_info = 2;
        elevator_uart1_thread->elevator_info_state.direction = 0;
        elevator_uart1_thread->elevator_info_state.floor_info = (debug_counter/100 +1);

        elevator_uart2_thread->elevator_info_state.spec_info = 2;
        elevator_uart2_thread->elevator_info_state.direction = 0;
        elevator_uart2_thread->elevator_info_state.floor_info = (debug_counter/100 +1);

    }

    if(debug_counter > 1600)
    {
        elevator_uart1_thread->elevator_info_state.spec_info = 1;
        elevator_uart1_thread->elevator_info_state.direction = 1;
        elevator_uart1_thread->elevator_info_state.floor_info = 34-(debug_counter/100 +1);

        elevator_uart2_thread->elevator_info_state.spec_info = 3;
        elevator_uart2_thread->elevator_info_state.direction = 1;
        elevator_uart2_thread->elevator_info_state.floor_info = 34-(debug_counter/100 +1);
    }

    if(debug_counter%100 == 0)
    {
        elevator_uart1_thread->elevator_info_state.elevator_state =1;
        emit(emit_update_uart_1());
        elevator_uart2_thread->elevator_info_state.elevator_state =1;
        emit(emit_update_uart_2());

    }
#endif
    if(debug_counter > 3200)
        debug_counter =1;
}


char MainWindow::do_system_verify()
{
    char tmp;
#if 0
    //qDebug("do system verify");

    if(elevator_uart1_thread->uart_fd < 0)
        elevator_uart1_thread->uart_fd=Init_serial(elevator_info_uart_1);
    if(elevator_uart1_thread->uart_fd < 0)
        return 0;
    tmp=system_verify();
    if(tmp ==1)
    {
        system_verify_flag = "verify passed";
        elevator_uart1_thread->start();
        if(elevator_num == "2")
            elevator_uart2_thread->start();
    }
#endif

}

#if 0
void MainWindow::on_pushButton_clicked()
{
}

#endif

void MainWindow::seek_video(void)
{
    qDebug("seek video");
    process0->write("seek 0 2\n");


}


void MainWindow::get_mplayer_pos()
{
    QString str00;
    str00 = process0->readAll();
    get_the_video_pos_0 = str00;
    //qDebug("%s",qPrintable(str00));
    mplayer_run_flag = 0;

}

void MainWindow::check_mplayer_stat()
{
    mplayer_run_flag++;
    if(mplayer_run_flag > 10)
    {
        if(process0->pid() != 0)
            process0->terminate();
        mplayer_run_flag = 0;
    }

    if(get_the_video_pos_0 == get_the_video_pos_1)
    {
        if(process0->pid() != 0)
            process0->terminate();
    }

    get_the_video_pos_1 = get_the_video_pos_0;
    if(process0->pid() != 0)
        process0->write("get_time_pos\n");
    if(QFile::exists(source_dir))
    {
        mplayer_run_flag = 0;
    }
    else
        QTimer::singleShot(2000,this,SLOT(check_mplayer_stat()));
}
